#include "Videogame_Process.h"
#include "Core.h"
#include "GraphicsManager.h"
#include "Math\Color.h"
#include "InputManager.h"
#include "InputDefs.h"
#include "FPSCamera.h"
#include "ThPSCamera.h"
#include "Math\Matrix44.h"
#include "Math\Vector3.h"

CVideogame_Process::CVideogame_Process(void)
{
}


CVideogame_Process::~CVideogame_Process(void)
{
}

void
CVideogame_Process::Init()
{
	m_pObjectCamara = new CObject3D(Vect3f(-5.0f, 1.0f, -5.0f), 0.0f, 0.0f, 0.0f);
	CProcess::SetpCamara(new CFPSCamera(0.01f, 1000.0f, 45.0f, 800.0f / 600.0f, m_pObjectCamara));
	CProcess::SetdCamara(new CThPSCamera(0.01f, 1000.0f, 45.0f, 800.0f / 600.0f, m_pObjectCamara, 5.0f));

  m_translation_angle = 0.0f;
  m_rotation_angle = 0.0f;
}

void
CVideogame_Process::DeInit()
{
	delete(m_pObjectCamara);
	m_pObjectCamara = 0;
}

void 
CVideogame_Process::Update()
{
	float p = m_pObjectCamara->GetPitch() - CCore::GetSingleton().GetinputManager()->GetMouseDelta().y * 0.01f;
	float y = m_pObjectCamara->GetYaw() - CCore::GetSingleton().GetinputManager()->GetMouseDelta().x * 0.01f;
	
	m_pObjectCamara->SetPitch(p);
	m_pObjectCamara->SetYaw(y);
	float dt = 0.01;

	if(CCore::GetSingleton().GetinputManager()->IsUpDown(IDV_KEYBOARD, KEY_TAB))
	{
		bool aux = GetactiveCamara();
		SetactiveCamara(!aux);
	}

  m_rotation_angle += 0.001f;
  m_translation_angle += 0.001f;

	if (!GetactiveCamara())
	{
		((CThPSCamera*)GetActiveCamara())->AddZoom(CCore::GetSingleton().GetinputManager()->GetMouseDelta().z * 0.01f);
	}
	else
	{
		if(CCore::GetSingleton().GetinputManager()->IsDown(IDV_KEYBOARD, KEY_UP))
		{
			m_pObjectCamara->SetPosition(m_pObjectCamara->GetPosition() + GetActiveCamara()->GetDirection() * dt);
		}
	
		if(CCore::GetSingleton().GetinputManager()->IsDown(IDV_KEYBOARD, KEY_DOWN))
		{
			m_pObjectCamara->SetPosition(m_pObjectCamara->GetPosition() - GetActiveCamara()->GetDirection() * dt);
		}

		if(CCore::GetSingleton().GetinputManager()->IsDown(IDV_KEYBOARD, KEY_LEFT))
		{
			m_pObjectCamara->SetPosition(m_pObjectCamara->GetPosition() - (GetActiveCamara()->GetVecUp() ^ GetActiveCamara()->GetDirection()) * dt);
		}

		if(CCore::GetSingleton().GetinputManager()->IsDown(IDV_KEYBOARD, KEY_RIGHT))
		{
			m_pObjectCamara->SetPosition(m_pObjectCamara->GetPosition() + (GetActiveCamara()->GetVecUp() ^ GetActiveCamara()->GetDirection()) * dt);
		}
	}
}

void 
CVideogame_Process::Render()
{
	CCore::GetSingleton().GetgraphicsManager()->DrawAxis(1);

  CCore::GetSingleton().GetgraphicsManager()->DrawCube(1,colYELLOW);

  Mat44f t;
	Mat44f rotation;
  Mat44f position;
  Mat44f translation;

  rotation.SetIdentity();
  position.SetIdentity();
  translation.SetIdentity();

  rotation.RotByAngleY(m_rotation_angle);
  position.Translate(Vect3f(5.0f,0.0f,0.0f));
  translation.RotByAngleY(m_translation_angle);

  t.SetIdentity();
  t = rotation*position*translation;
  CCore::GetSingleton().GetgraphicsManager()->SetTransform(t);
  CCore::GetSingleton().GetgraphicsManager()->DrawCube(1,colWHITE);

  rotation.SetIdentity();
  position.SetIdentity();
  translation.SetIdentity();

  rotation.RotByAngleY(m_rotation_angle);
  position.Translate(Vect3f(1.0f,0.0f,0.0f));
  translation.RotByAngleY(m_translation_angle);

  t.SetIdentity();
  t = rotation*position*translation;
  CCore::GetSingleton().GetgraphicsManager()->SetTransform(t);
  CCore::GetSingleton().GetgraphicsManager()->DrawCube(0.5,colWHITE);

	t.SetIdentity();
	t.Translate(m_pObjectCamara->GetPosition());
	CCore::GetSingleton().GetgraphicsManager()->SetTransform(t);
	CCore::GetSingleton().GetgraphicsManager()->DrawAxis(0.5f);

	t.SetIdentity();
	CCore::GetSingleton().GetgraphicsManager()->SetTransform(t);

	//CCore::GetSingleton().GetgraphicsManager()->DrawAxis(1);
	//CCore::GetSingleton().GetgraphicsManager()->DrawCube(1);
	//CCore::GetSingleton().GetgraphicsManager()->DrawBox(1.2,2,1,colBLACK);
	//CCore::GetSingleton().GetgraphicsManager()->DrawGrid(3,colYELLOW, 10, 10);
	//CCore::GetSingleton().GetgraphicsManager()->DrawPlane(4, Vect3f(0.3,0,0), 1);
}